package cc.vant.wechat;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import cc.vant.wechat.fragment.ContactFragment;
import cc.vant.wechat.fragment.WechatFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mBtnWeChat;
    private ImageView mBtnContact;
    private ImageView mBtnFind;
    private ImageView mBtnMe;


    private WechatFragment mWeChatFragment = null;
    private ContactFragment mContactFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_new);

        mBtnWeChat = findViewById(R.id.btn_wechat);
        mBtnContact = findViewById(R.id.btn_contact);
        mBtnFind = findViewById(R.id.btn_find);
        mBtnMe = findViewById(R.id.btn_me);

        findViewById(R.id.bottom_bar_wechat).setOnClickListener(this);
        findViewById(R.id.bottom_bar_contact).setOnClickListener(this);
        findViewById(R.id.bottom_bar_find).setOnClickListener(this);
        findViewById(R.id.bottom_bar_me).setOnClickListener(this);

        selectTab(0);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bottom_bar_wechat:
                selectTab(0);
                break;
            case R.id.bottom_bar_contact:
                selectTab(1);
                break;
            case R.id.bottom_bar_find:
                selectTab(2);
                break;
            case R.id.bottom_bar_me:
                selectTab(3);
                break;
            default:
                Log.e("MainActivity", "on click error!");
        }
    }

    private void selectTab(int index) {
        setTabButtonDefaultColor();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case 0:
                mBtnWeChat.setImageResource(R.mipmap.wechat12);
                if (mWeChatFragment == null) {
                    mWeChatFragment = new WechatFragment();
                    transaction.add(R.id.ll_frame, mWeChatFragment);
                } else {
                    transaction.show(mWeChatFragment);
                }
                break;
            case 1:
                mBtnContact.setImageResource(R.mipmap.wechat22);
                if (mContactFragment == null) {
                    mContactFragment = new ContactFragment();
                    transaction.add(R.id.ll_frame, mContactFragment);
                } else {
                    transaction.show(mContactFragment);
                }
                break;
            case 2:
                mBtnFind.setImageResource(R.mipmap.wechat32);
                break;
            case 3:
                mBtnMe.setImageResource(R.mipmap.wechat42);
                break;
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mWeChatFragment != null) {
            transaction.hide(mWeChatFragment);
        }

        if (mContactFragment != null) {
            transaction.hide(mContactFragment);
        }
    }

    private void setTabButtonDefaultColor() {
        mBtnWeChat.setImageResource(R.mipmap.wechat1);
        mBtnContact.setImageResource(R.mipmap.wechat2);
        mBtnFind.setImageResource(R.mipmap.wechat3);
        mBtnMe.setImageResource(R.mipmap.wechat4);
    }
}